import nodeResolve from 'rollup-plugin-node-resolve';
import { eslint } from 'rollup-plugin-eslint';
import commonjs from 'rollup-plugin-commonjs';
import { terser } from 'rollup-plugin-terser';
import babel from 'rollup-plugin-babel';
import replace from 'rollup-plugin-replace';
import postcss from 'rollup-plugin-postcss';

// load postcss plugins
import simplevars from 'postcss-simple-vars';
import nested from 'postcss-nested';
import cssnext from 'postcss-cssnext';
import cssnano from 'cssnano';

// hot reload
import serve from 'rollup-plugin-serve';
import livereload from 'rollup-plugin-livereload';

// HTML generator
import htmlTemplate from 'rollup-plugin-generate-html-template';
import html from 'rollup-plugin-html'

const env = process.env.NODE_ENV,
      isProduction = env === 'production',
      isDevelopment = env === 'development';
const config = {
  input: 'src/main.js',
  output: {
    file: 'dist/bundle.js',
    sourcemap: isDevelopment ? 'inline' : false,
    format: 'umd',
  },
  plugins: [
    htmlTemplate({
      template: 'src/template.html',
      target: 'index.html',
    }),
    html(),
    isProduction ? null : serve({
      contentBase: ['dist'],
    }),
    isProduction ? null : livereload({
      watch: 'dist',
    }),
    postcss({
      plugins: [
        simplevars(),
        nested(),
        cssnext(),
        cssnano(),
      ],
      extensions: ['.css'],
    }),
    nodeResolve({
      jsnext: true,
      main: true,
    }),
    eslint({
      exclude: [
        'src/styles/**',
        'src/**/*.html'
      ],
    }),
    babel({
      exclude: 'node_modules/**',
    }),
    commonjs(),
    replace({
      'process.env.NODE_ENV': JSON.stringify(env)
    }),
    process.env.NODE_ENV === 'production' ? terser() : null,
  ],
};

export default config;
